#include <stdio.h>
#include <stdlib.h>

#include "html.h"
#include "utils.h"

/* globals */
extern char *line;
extern char *params[9];
extern int nparams;
extern int inparagraph;


int
spitheader(void)
{
	const char htmlheader[] =
		"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
		"\"http://www.w3.org/TR/html4/loose.dtd\">\n"
		"<html>\n"
		"<head>\n"
		"<meta name=\"generator\" content=\"webgen, see domonkos.tk\">\n"
		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=US-ASCII\">\n"
		"<meta name=\"Content-Style\" content=\"text/css\">\n"
		"<title>Personal website</title>\n"
		"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n"
		"</head>\n"
		"<body>\n";

	return fputs(htmlheader, stdout);
}

int spitfooter(void)
{
	const char htmlfooter[] =
		"<hr>\n"
		"</body>\n"
		"</html>\n";

	return fputs(htmlfooter, stdout);

}


int
spithref(void)
{
	const char htmlrefstring[] = "<a href=\"%s\">%s</a>";

	if (nparams < 1) {
		err("href requires at least one parameter!");
		return 1;
	} else if (nparams == 1) {
		printf(htmlrefstring, params[0], params[0]);
	} else if (nparams == 2) {
		printf(htmlrefstring, params[0], params[1]);
	} else if (nparams > 2) {
		err("href takes two parameters at max!");
		return 1;
	}

	return 0;
}

int
spitimg(void)
{
	const char htmlimgstring[] = "<img src=\"%s\" alt=\"Image %s\">";

	if (nparams < 1) {
		err("img requires at least one parameter!");
		return 1;
	} else if (nparams == 1) {
		printf(htmlimgstring, params[0], params[0]);
	} else if (nparams == 2) {
		printf(htmlimgstring, params[0], params[1]);
	} else if (nparams > 2) {
		err("img takes two parameters at max!");
		return 1;
	}

	return 0;
}

int
spititalic(void)
{
	if (nparams < 1) {
		err("italic requires at least one parameter!");
		return 1;
	} else if (nparams == 1) {
		printf("<em>%s</em>\n", params[0]);
	} else if (nparams == 2) {
		printf("<em>%s</em>%s\n", params[0], params[1]);
	} else if (nparams == 3) {
		printf("%s<em>%s</em>%s\n", params[2], params[0], params[1]);
	} else if (nparams > 3) {
		err("italic takes three parameters at max!");
		return 1;
	}

	return 0;
}

int
spitbold(void)
{
	if (nparams < 1) {
		err("bold requires at least one parameter!");
		return 1;
	} else if (nparams == 1) {
		printf("<b>%s</b>\n", params[0]);
	} else if (nparams == 2) {
		printf("<b>%s</b>%s\n", params[0], params[1]);
	} else if (nparams == 3) {
		printf("%s<b>%s</b>%s\n", params[2], params[0], params[1]);
	} else if (nparams > 3) {
		err("bold takes three parameters at max!");
		return 1;
	}

	return 0;
}

int
spitmonospace(void)
{
	if (nparams < 1) {
		err("monospace requires at least one parameter!");
		return 1;
	} else if (nparams == 1) {
		printf("<tt>%s</tt>\n", params[0]);
	} else if (nparams == 2) {
		printf("<tt>%s</tt>%s\n", params[0], params[1]);
	} else if (nparams == 3) {
		printf("%s<tt>%s</tt>%s\n", params[2], params[0], params[1]);
	} else if (nparams > 3) {
		err("monospace takes three parameters at max!");
		return 1;
	}

	return 0;
}

int
spitlinebreak(void)
{
	if (nparams >= 1) {
		err("br takes no params!");
		return 1;
	} else {
		puts("<br>");
	}

	return 0;
}

int
newparagraph(void)
{
	/* if (nparams >= 1) { //TODO
		err("paragraph takes no params!");
		return 1;
	} */
	if (inparagraph) {
		puts("</p>\n<p>");
	} else {
		puts("<p>");
	}

	inparagraph = 1;

	return 0;
}

int
spithorizontalrule(void)
{
	if (nparams >= 1) {
		err("hr takes no params!");
		return 1;
	} else {
		puts("<hr>");
	}

	return 0;
}

int
subheading(void)
{
	int level = 1;

	if (nparams < 2) {
		printf("<h1>%s</h1>\n", params[0]);
	} else if (nparams == 2) {
		level = atoi(params[0]);
		if (level < 1 || level > 6) {
			err("subheading level must be between [1 6]!");
			return 1;
		}
		printf("<h%d>%s</h%d>\n", level, params[1], level);
	} else { // nparams > 2
		err("subheading takes two parameters at max!");
		return 1;
	}

	return 0;
}

int
codestart(void)
{
	if (nparams >= 1) {
		err(".cs takes no params!");
		return 1;
	} else {
		puts("<pre><code>");
	}

	return 0;
}

int
codeend(void)
{
	if (nparams >= 1) {
		err(".ce takes no params!");
		return 1;
	} else {
		puts("</pre></code>");
	}

	return 0;
}
